package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//提供Restful服务
@RestController
//@RequestMapping("/DeptController")
public class DeptController {

    @Autowired
    private DeptService deptService;


    //获取一些配置的信息，得到具体的微服务
    @Autowired
    private DiscoveryClient client;

    //添加部门
    @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
    public boolean addDept(@RequestBody Dept dept) {
        System.out.println("8011端口添加");
        return deptService.addDept(dept);
    }

    //根据id查询部门
    @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
    public Dept queryById(@PathVariable("id") Integer id) {
        System.out.println("8011端口查询id");
        return deptService.queryById(id);
    }

    //查询全部部门
    @RequestMapping(value = "/queryAll", method = RequestMethod.GET)
    public List<Dept> queryAll() {
        System.out.println("8011端口查询全部");

        return deptService.queryAll();
    }

    //注册进来的微服务~获取一些消息
    @RequestMapping(value = "/dept/discovery",method = RequestMethod.GET)
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services:::" + services);
        //得到一个具体的微服务信息,通过具体的微服务id，application的Name
        List<ServiceInstance> instances = client.getInstances("SPRING-CLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println("主机名称::" + instance.getHost() + "     端口号::" + instance.getPort()
                    + "     获取url::" + instance.getUri() + "     获取实例的名字::" + instance.getServiceId()
            );
        }
        return this.client;
    }
}
